/*
 * PrevisaoCadastrar.java
 *
 * Created on 06/03/2009, 08:25:35
 * Copyright Jaime Junior
 * Modify on 15/09/2009
 * by Elizabete
 */
package Adecot.Previsões;

import Controller.Facade.*;
import Adecot.*;
import Controller.AssociadoBO;
import Controller.Facade.IceDecorator;
import Controller.ItemPrevisaoBO;
import Controller.PrevisaoBO;
import Controller.ProdutoBO;
import Model.*;
import Model.BD.DAO;
import Model.ItemPrevisao;
import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlDataTable;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.HtmlMessage;
import com.icesoft.faces.component.ext.HtmlOutputText;
import com.icesoft.faces.component.ext.HtmlSelectOneMenu;
import com.icesoft.faces.component.ext.UIColumn;
import com.icesoft.faces.component.jsfcl.data.DefaultSelectedData;
import com.icesoft.faces.component.jsfcl.data.DefaultSelectionItems;
import com.icesoft.faces.component.jsfcl.data.DefaultTableDataModel;
import com.icesoft.faces.component.jsfcl.data.PopupBean;
import com.icesoft.faces.component.jsfcl.data.SelectInputDateBean;
import com.icesoft.faces.component.panellayout.PanelLayout;
import com.icesoft.faces.component.panelpopup.PanelPopup;
import com.icesoft.faces.component.selectinputdate.SelectInputDate;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.faces.FacesException;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.CharacterConverter;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import javax.faces.validator.ValidatorException;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 */
public class PrevisaoCadastrar extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    private int __placeholder;
    private boolean parar;

   public boolean isParar() {
      return parar;
   }

   public void setParar(boolean parar) {
      this.parar = parar;
   }

    private void _init() throws Exception {
    }
    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private static List<ItemPrevisao> listaExclusao = new ArrayList<ItemPrevisao>();
    private List<ItemPrevisao> items = new ArrayList<ItemPrevisao>();
    private SelectInputDateBean dataSelecionada = new SelectInputDateBean();

    public SelectInputDateBean getDataSelecionada() {
        return dataSelecionada;
    }

    public void setDataSelecionada(SelectInputDateBean sidb) {
        this.dataSelecionada = sidb;
    }
    private SelectInputDate campoDataInicio = new SelectInputDate();

    public SelectInputDate getCampoDataInicio() {
        return campoDataInicio;
    }

    public void setCampoDataInicio(SelectInputDate sid) {
        this.campoDataInicio = sid;
    }
    private HtmlInputText campoQuantidade = new HtmlInputText();

    public HtmlInputText getCampoQuantidade() {
        return campoQuantidade;
    }

    public void setCampoQuantidade(HtmlInputText hit) {
        this.campoQuantidade = hit;
    }
    private DefaultSelectedData associadoSelecionado = new DefaultSelectedData();

    public DefaultSelectedData getAssociadoSelecionado() {
        return associadoSelecionado;
    }

    public void setAssociadoSelecionado(DefaultSelectedData dsd) {
        this.associadoSelecionado = dsd;
    }
    private DefaultSelectionItems associados = new DefaultSelectionItems();

    public DefaultSelectionItems getAssociados() {
        return associados;
    }

    public void setAssociados(DefaultSelectionItems dsi) {
        this.associados = dsi;
    }
    private HtmlSelectOneMenu comboAssociado = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getComboAssociado() {
        return comboAssociado;
    }

    public void setComboAssociado(HtmlSelectOneMenu hsom) {
        this.comboAssociado = hsom;
    }
    private DefaultSelectedData produtoSelecionado = new DefaultSelectedData();

    public DefaultSelectedData getProdutoSelecionado() {
        return produtoSelecionado;
    }

    public void setProdutoSelecionado(DefaultSelectedData dsd) {
        this.produtoSelecionado = dsd;
    }
    private DefaultSelectionItems produtos = new DefaultSelectionItems();

    public DefaultSelectionItems getProdutos() {
        return produtos;
    }

    public void setProdutos(DefaultSelectionItems dsi) {
        this.produtos = dsi;
    }
    private HtmlOutputText msgPrev = new HtmlOutputText();

    public HtmlOutputText getMsgPrev() {
        return msgPrev;
    }

    public void setMsgPrev(HtmlOutputText hot) {
        this.msgPrev = hot;
    }
    private UIColumn colunaProduto = new UIColumn();

    public UIColumn getColunaProduto() {
        return colunaProduto;
    }

    public void setColunaProduto(UIColumn uic) {
        this.colunaProduto = uic;
    }
    private javax.faces.component.html.HtmlInputText tabelaTextoProduto = new javax.faces.component.html.HtmlInputText();

    public javax.faces.component.html.HtmlInputText getTabelaTextoProduto() {
        return tabelaTextoProduto;
    }

    public void setTabelaTextoProduto(javax.faces.component.html.HtmlInputText hit) {
        this.tabelaTextoProduto = hit;
    }
    private PopupBean painelPopupAviso = new PopupBean();

    public PopupBean getPainelPopupAviso() {
        return painelPopupAviso;
    }

    public void setPainelPopupAviso(PopupBean pb) {
        this.painelPopupAviso = pb;
    }
    private HtmlOutputText msgAviso = new HtmlOutputText();

    public HtmlOutputText getMsgAviso() {
        return msgAviso;
    }

    public void setMsgAviso(HtmlOutputText hot) {
        this.msgAviso = hot;
    }
    private HtmlOutputText mensagem1 = new HtmlOutputText();

    public HtmlOutputText getMensagem1() {
        return mensagem1;
    }

    public void setMensagem1(HtmlOutputText hot) {
        this.mensagem1 = hot;
    }
    private PanelPopup painelAviso = new PanelPopup();

    public PanelPopup getPainelAviso() {
        return painelAviso;
    }

    public void setPainelAviso(PanelPopup pp) {
        this.painelAviso = pp;
    }
    private HtmlOutputText outputText1 = new HtmlOutputText();

    public HtmlOutputText getOutputText1() {
        return outputText1;
    }

    public void setOutputText1(HtmlOutputText hot) {
        this.outputText1 = hot;
    }
    private PopupBean painelPopupSair = new PopupBean();

    public PopupBean getPainelPopupSair() {
        return painelPopupSair;
    }

    public void setPainelPopupSair(PopupBean pb) {
        this.painelPopupSair = pb;
    }
    private HtmlOutputText mensagem = new HtmlOutputText();

    public HtmlOutputText getMensagem() {
        return mensagem;
    }

    public void setMensagem(HtmlOutputText hot) {
        this.mensagem = hot;
    }
    private PopupBean painelPopupMensagem = new PopupBean();

    public PopupBean getPainelPopupMensagem() {
        return painelPopupMensagem;
    }

    public void setPainelPopupMensagem(PopupBean pb) {
        this.painelPopupMensagem = pb;
    }
    private HtmlOutputText msgTeste = new HtmlOutputText();

    public HtmlOutputText getMsgTeste() {
        return msgTeste;
    }

    public void setMsgTeste(HtmlOutputText hot) {
        this.msgTeste = hot;
    }
    private HtmlOutputText msgErro = new HtmlOutputText();

    public HtmlOutputText getMsgErro() {
        return msgErro;
    }

    public void setMsgErro(HtmlOutputText hot) {
        this.msgErro = hot;
    }
    private PanelPopup painelMensagem = new PanelPopup();

    public PanelPopup getPainelMensagem() {
        return painelMensagem;
    }

    public void setPainelMensagem(PanelPopup pp) {
        this.painelMensagem = pp;
    }
    private PanelPopup painelSair = new PanelPopup();

    public PanelPopup getPainelSair() {
        return painelSair;
    }

    public void setPainelSair(PanelPopup pp) {
        this.painelSair = pp;
    }
    private PanelLayout painelCorpo = new PanelLayout();

    public PanelLayout getPainelCorpo() {
        return painelCorpo;
    }

    public void setPainelCorpo(PanelLayout pl) {
        this.painelCorpo = pl;
    }
    private PanelPopup painelConsulta = new PanelPopup();

    public PanelPopup getPainelConsulta() {
        return painelConsulta;
    }

    public void setPainelConsulta(PanelPopup pp) {
        this.painelConsulta = pp;
    }
    private HtmlCommandButton botaoNovo = new HtmlCommandButton();

    public HtmlCommandButton getBotaoNovo() {
        return botaoNovo;
    }

    public void setBotaoNovo(HtmlCommandButton hcb) {
        this.botaoNovo = hcb;
    }
    private HtmlCommandButton botaoConsultar = new HtmlCommandButton();

    public HtmlCommandButton getBotaoConsultar() {
        return botaoConsultar;
    }

    public void setBotaoConsultar(HtmlCommandButton hcb) {
        this.botaoConsultar = hcb;
    }
    private HtmlCommandButton botaoAlterar = new HtmlCommandButton();

    public HtmlCommandButton getBotaoAlterar() {
        return botaoAlterar;
    }

    public void setBotaoAlterar(HtmlCommandButton hcb) {
        this.botaoAlterar = hcb;
    }
    private HtmlCommandButton botaoImprimir = new HtmlCommandButton();

    public HtmlCommandButton getBotaoImprimir() {
        return botaoImprimir;
    }

    public void setBotaoImprimir(HtmlCommandButton hcb) {
        this.botaoImprimir = hcb;
    }
    private HtmlCommandButton botaoCadastrar = new HtmlCommandButton();

    public HtmlCommandButton getBotaoCadastrar() {
        return botaoCadastrar;
    }

    public void setBotaoCadastrar(HtmlCommandButton hcb) {
        this.botaoCadastrar = hcb;
    }
    private HtmlCommandButton botaoLimpar = new HtmlCommandButton();

    public HtmlCommandButton getBotaoLimpar() {
        return botaoLimpar;
    }

    public void setBotaoLimpar(HtmlCommandButton hcb) {
        this.botaoLimpar = hcb;
    }
    private HtmlCommandButton botaoAdicionarProduto = new HtmlCommandButton();

    public HtmlCommandButton getBotaoAdicionarProduto() {
        return botaoAdicionarProduto;
    }

    public void setBotaoAdicionarProduto(HtmlCommandButton hcb) {
        this.botaoAdicionarProduto = hcb;
    }
    private HtmlCommandButton botaoSairSim = new HtmlCommandButton();

    public HtmlCommandButton getBotaoSairSim() {
        return botaoSairSim;
    }

    public void setBotaoSairSim(HtmlCommandButton hcb) {
        this.botaoSairSim = hcb;
    }
    private HtmlCommandButton botaoSairNao = new HtmlCommandButton();

    public HtmlCommandButton getBotaoSairNao() {
        return botaoSairNao;
    }

    public void setBotaoSairNao(HtmlCommandButton hcb) {
        this.botaoSairNao = hcb;
    }
    private HtmlCommandButton botaoAtencaoOk = new HtmlCommandButton();

    public HtmlCommandButton getBotaoAtencaoOk() {
        return botaoAtencaoOk;
    }

    public void setBotaoAtencaoOk(HtmlCommandButton hcb) {
        this.botaoAtencaoOk = hcb;
    }
    private HtmlCommandButton botaoAvisoSim = new HtmlCommandButton();

    public HtmlCommandButton getBotaoAvisoSim() {
        return botaoAvisoSim;
    }

    public void setBotaoAvisoSim(HtmlCommandButton hcb) {
        this.botaoAvisoSim = hcb;
    }
    private HtmlCommandButton botaoAvisoNao = new HtmlCommandButton();

    public HtmlCommandButton getBotaoAvisoNao() {
        return botaoAvisoNao;
    }

    public void setBotaoAvisoNao(HtmlCommandButton hcb) {
        this.botaoAvisoNao = hcb;
    }
    private HtmlCommandButton botaoConsultaCancelar = new HtmlCommandButton();

    public HtmlCommandButton getBotaoConsultaCancelar() {
        return botaoConsultaCancelar;
    }

    public void setBotaoConsultaCancelar(HtmlCommandButton hcb) {
        this.botaoConsultaCancelar = hcb;
    }
    private HtmlCommandButton botaoConsultaOk = new HtmlCommandButton();

    public HtmlCommandButton getBotaoConsultaOk() {
        return botaoConsultaOk;
    }

    public void setBotaoConsultaOk(HtmlCommandButton hcb) {
        this.botaoConsultaOk = hcb;
    }
    private HtmlCommandButton botaoSair = new HtmlCommandButton();

    public HtmlCommandButton getBotaoSair() {
        return botaoSair;
    }

    public void setBotaoSair(HtmlCommandButton hcb) {
        this.botaoSair = hcb;
    }
    private HtmlSelectOneMenu comboProduto = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getComboProduto() {
        return comboProduto;
    }

    public void setComboProduto(HtmlSelectOneMenu hsom) {
        this.comboProduto = hsom;
    }
    private SelectInputDate campoDataFim = new SelectInputDate();

    public SelectInputDate getCampoDataFim() {
        return campoDataFim;
    }

    public void setCampoDataFim(SelectInputDate sid) {
        this.campoDataFim = sid;
    }
    private HtmlOutputText msgAvisoResultado = new HtmlOutputText();

    public HtmlOutputText getMsgAvisoResultado() {
        return msgAvisoResultado;
    }

    public void setMsgAvisoResultado(HtmlOutputText hot) {
        this.msgAvisoResultado = hot;
    }
    private HtmlOutputText msgAvisoPergunta = new HtmlOutputText();

    public HtmlOutputText getMsgAvisoPergunta() {
        return msgAvisoPergunta;
    }

    public void setMsgAvisoPergunta(HtmlOutputText hot) {
        this.msgAvisoPergunta = hot;
    }
    private HtmlOutputText msgMensagemResultado = new HtmlOutputText();

    public HtmlOutputText getMsgMensagemResultado() {
        return msgMensagemResultado;
    }

    public void setMsgMensagemResultado(HtmlOutputText hot) {
        this.msgMensagemResultado = hot;
    }
    private HtmlCommandButton botaoMensagemOk = new HtmlCommandButton();

    public HtmlCommandButton getBotaoMensagemOk() {
        return botaoMensagemOk;
    }

    public void setBotaoMensagemOk(HtmlCommandButton hcb) {
        this.botaoMensagemOk = hcb;
    }
    private DefaultTableDataModel dataTable1Model = new DefaultTableDataModel();

    public DefaultTableDataModel getDataTable1Model() {
        return dataTable1Model;
    }

    public void setDataTable1Model(DefaultTableDataModel dtdm) {
        this.dataTable1Model = dtdm;
    }
    private javax.faces.component.html.HtmlOutputText classificacao = new javax.faces.component.html.HtmlOutputText();

    public javax.faces.component.html.HtmlOutputText getClassificacao() {
        return classificacao;
    }

    public void setClassificacao(javax.faces.component.html.HtmlOutputText hot) {
        this.classificacao = hot;
    }
    private javax.faces.component.html.HtmlCommandButton botaoTabelaExcluir = new javax.faces.component.html.HtmlCommandButton();

    public javax.faces.component.html.HtmlCommandButton getBotaoTabelaExcluir() {
        return botaoTabelaExcluir;
    }

    public void setBotaoTabelaExcluir(javax.faces.component.html.HtmlCommandButton hcb) {
        this.botaoTabelaExcluir = hcb;
    }
    private javax.faces.component.html.HtmlOutputText variedade = new javax.faces.component.html.HtmlOutputText();

    public javax.faces.component.html.HtmlOutputText getVariedade() {
        return variedade;
    }

    public void setVariedade(javax.faces.component.html.HtmlOutputText hot) {
        this.variedade = hot;
    }
    private HtmlDataTable tabelaPadrao = new HtmlDataTable();

    public HtmlDataTable getTabelaPadrao() {
        return tabelaPadrao;
    }

    public void setTabelaPadrao(HtmlDataTable hdt) {
        this.tabelaPadrao = hdt;
    }
    private javax.faces.component.html.HtmlInputText campoTabelaQuantidadeMudar = new javax.faces.component.html.HtmlInputText();

    public javax.faces.component.html.HtmlInputText getCampoTabelaQuantidadeMudar() {
        return campoTabelaQuantidadeMudar;
    }

    public void setCampoTabelaQuantidadeMudar(javax.faces.component.html.HtmlInputText hit) {
        this.campoTabelaQuantidadeMudar = hit;
    }
    private javax.faces.component.html.HtmlOutputText campoTabelaQuantidade = new javax.faces.component.html.HtmlOutputText();

    public javax.faces.component.html.HtmlOutputText getCampoTabelaQuantidade() {
        return campoTabelaQuantidade;
    }

    public void setCampoTabelaQuantidade(javax.faces.component.html.HtmlOutputText hot) {
        this.campoTabelaQuantidade = hot;
    }
    private HtmlOutputText infoAssociado = new HtmlOutputText();

    public HtmlOutputText getInfoAssociado() {
        return infoAssociado;
    }

    public void setInfoAssociado(HtmlOutputText hot) {
        this.infoAssociado = hot;
    }
    private SelectInputDateBean selectInputDate1Bean = new SelectInputDateBean();

    public SelectInputDateBean getSelectInputDate1Bean() {
        return selectInputDate1Bean;
    }

    public void setSelectInputDate1Bean(SelectInputDateBean sidb) {
        this.selectInputDate1Bean = sidb;
    }
    private SelectInputDate campoTabelaDataFim = new SelectInputDate();

    public SelectInputDate getCampoTabelaDataFim() {
        return campoTabelaDataFim;
    }

    public void setCampoTabelaDataFim(SelectInputDate sid) {
        this.campoTabelaDataFim = sid;
    }
    private SelectInputDateBean selectInputDateBean3 = new SelectInputDateBean();

    public SelectInputDateBean getSelectInputDateBean3() {
        return selectInputDateBean3;
    }

    public void setSelectInputDateBean3(SelectInputDateBean sidb) {
        this.selectInputDateBean3 = sidb;
    }
    private SelectInputDate campoTabelaDataInicio = new SelectInputDate();

    public SelectInputDate getCampoTabelaDataInicio() {
        return campoTabelaDataInicio;
    }

    public void setCampoTabelaDataInicio(SelectInputDate sid) {
        this.campoTabelaDataInicio = sid;
    }
    private CharacterConverter characterConverter1 = new CharacterConverter();

    public CharacterConverter getCharacterConverter1() {
        return characterConverter1;
    }

    public void setCharacterConverter1(CharacterConverter cc) {
        this.characterConverter1 = cc;
    }
    private HtmlMessage msgQuantidade = new HtmlMessage();

    public HtmlMessage getMsgQuantidade() {
        return msgQuantidade;
    }

    public void setMsgQuantidade(HtmlMessage hm) {
        this.msgQuantidade = hm;
    }
    private PopupBean panelPopup1Bean = new PopupBean();

    public PopupBean getPanelPopup1Bean() {
        return panelPopup1Bean;
    }

    public void setPanelPopup1Bean(PopupBean pb) {
        this.panelPopup1Bean = pb;
    }
    private DefaultSelectedData associadoSelecionadoConsulta = new DefaultSelectedData();

    public DefaultSelectedData getAssociadoSelecionadoConsulta() {
        return associadoSelecionadoConsulta;
    }

    public void setAssociadoSelecionadoConsulta(DefaultSelectedData dsd) {
        this.associadoSelecionadoConsulta = dsd;
    }
    private DefaultSelectionItems associadosConsulta = new DefaultSelectionItems();

    public DefaultSelectionItems getAssociadosConsulta() {
        return associadosConsulta;
    }

    public void setAssociadosConsulta(DefaultSelectionItems dsi) {
        this.associadosConsulta = dsi;
    }
    private HtmlSelectOneMenu comboAssociadoConsulta = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getComboAssociadoConsulta() {
        return comboAssociadoConsulta;
    }

    public void setComboAssociadoConsulta(HtmlSelectOneMenu hsom) {
        this.comboAssociadoConsulta = hsom;
    }
    private DefaultSelectedData previsaoSelecionadaConsulta = new DefaultSelectedData();

    public DefaultSelectedData getPrevisaoSelecionadaConsulta() {
        return previsaoSelecionadaConsulta;
    }

    public void setPrevisaoSelecionadaConsulta(DefaultSelectedData dsd) {
        this.previsaoSelecionadaConsulta = dsd;
    }
    private DefaultSelectionItems previsoesConsulta = new DefaultSelectionItems();

    public DefaultSelectionItems getPrevisoesConsulta() {
        return previsoesConsulta;
    }

    public void setPrevisoesConsulta(DefaultSelectionItems dsi) {
        this.previsoesConsulta = dsi;
    }
    private HtmlSelectOneMenu comboPrevisaoConsulta = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getComboPrevisaoConsulta() {
        return comboPrevisaoConsulta;
    }

    public void setComboPrevisaoConsulta(HtmlSelectOneMenu hsom) {
        this.comboPrevisaoConsulta = hsom;
    }
    private PopupBean panelPopup1Bean1 = new PopupBean();

    public PopupBean getPanelPopup1Bean1() {
        return panelPopup1Bean1;
    }

    public void setPanelPopup1Bean1(PopupBean pb) {
        this.panelPopup1Bean1 = pb;
    }
    private HtmlCommandButton botaoErroOk = new HtmlCommandButton();

    public HtmlCommandButton getBotaoErroOk() {
        return botaoErroOk;
    }

    public void setBotaoErroOk(HtmlCommandButton hcb) {
        this.botaoErroOk = hcb;
    }
    private UIColumn column2 = new UIColumn();

    public UIColumn getColumn2() {
        return column2;
    }

    public void setColumn2(UIColumn uic) {
        this.column2 = uic;
    }
    private UIColumn column3 = new UIColumn();

    public UIColumn getColumn3() {
        return column3;
    }

    public void setColumn3(UIColumn uic) {
        this.column3 = uic;
    }
    private UIColumn column4 = new UIColumn();

    public UIColumn getColumn4() {
        return column4;
    }

    public void setColumn4(UIColumn uic) {
        this.column4 = uic;
    }
    private UIColumn column1 = new UIColumn();

    public UIColumn getColumn1() {
        return column1;
    }

    public void setColumn1(UIColumn uic) {
        this.column1 = uic;
    }


    // </editor-fold>
    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public PrevisaoCadastrar() {
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     *
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here

        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("PrevisaoCadastrar Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }
        voltaCombo();
        setProdutos(new IceDecorator(ProdutoBO.class).montaCombo());
        previsoesConsulta.clear();
        panelPopup1Bean1.setShowDraggablePanel(false);

        //desabilita os pop-ups
        painelConsulta.setRendered(false);
        painelSair.setRendered(false);
        painelAviso.setRendered(false);
        painelMensagem.setRendered(false);
        //dataTable1Model.setWrappedData(null);
        //limpa campos e configura padrões da página
        limparCampos();
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
    }

    /**
     * <p>Retornar uma referência para o Bean de dados limitados.</p>
     *
     * @retorna uma referência para o bean de dados limitados
     */
    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    /**
     * <p>Retornar uma referência para o Bean de dados limitados.</p>
     *
     * @retorna uma referência para o bean de dados limitados
     */
    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    /**
     * <p>Retornar uma referência para o Bean de dados limitados.</p>
     *
     * @retorna uma referência para o bean de dados limitados
     */
    protected RequestBean1 getRequestBean1() {
        return (RequestBean1) getBean("RequestBean1");
    }

    //métodos para uso das telas
    public void desabilitarCampos() {
        campoDataFim.setDisabled(true);
        campoDataInicio.setDisabled(true);
        campoQuantidade.setDisabled(true);
        comboProduto.setDisabled(true);
        comboAssociado.setDisabled(true);
    }

    public void habilitarCampos() {
        campoDataFim.setDisabled(false);
        campoDataInicio.setDisabled(false);
        campoQuantidade.setDisabled(false);
        comboProduto.setDisabled(false);
        comboAssociado.setDisabled(false);
        botaoCadastrar.setDisabled(false);
    }
    public void voltaCombo(){
            associados.clear();
            setAssociados(new IceDecorator(AssociadoBO.class).montaCombo());
            associados.add(0, new SelectItem("0", "Selecione"));
            comboAssociado.setValue(0);
        }

    /**
     * monta tabela com valores do associado
     */
    public void montaTabelaAssociado() {
        try {
            FacesContext.getCurrentInstance().getExternalContext().getRequestMap().remove("Previsao");
            Previsao previsao = new Previsao();
            try {
                previsao = (Previsao) new IceDecorator(PrevisaoBO.class).retornaObjeto("codPrevisao",
                        Integer.parseInt(comboPrevisaoConsulta.getValue().toString()));
                setBean("Previsao", previsao);                
                campoDataFim.setValue(previsao.getDataFim());
                campoDataInicio.setValue(previsao.getDataInicio());
                campoTabelaDataInicio.setValue(previsao.getDataInicio());
                campoTabelaDataFim.setValue(previsao.getDataFim());


            } catch (Exception e) {
                campoDataInicio.resetValue();
                campoDataFim.resetValue();
                previsao = (Previsao) getBean("Previsao");                
            }
            dataTable1Model.setWrappedData(previsao.getItemPrevisao());
        } catch (Exception e) {
        }
    }

    public void montaTabelaAssociadoCadastra() {
        try {
            FacesContext.getCurrentInstance().getExternalContext().getRequestMap().remove("Previsao");
            Previsao previsao = new Previsao();
            try {
                previsao = (Previsao) new IceDecorator(PrevisaoBO.class).buscaObjetoFK("associado", "codAssociado",
                        Integer.parseInt(comboAssociado.getValue().toString()));
                campoDataFim.setValue(previsao.getDataFim());
                campoDataInicio.setValue(previsao.getDataInicio());

            } catch (Exception e) {
                campoDataInicio.resetValue();
                campoDataFim.resetValue();
                previsao = (Previsao) getBean("Previsao");
                Associado associado = (Associado) new IceDecorator(AssociadoBO.class).retornaObjetoCodigo("codAssociado", Integer.parseInt(comboAssociado.getValue().toString()));
                previsao.setAssociado(associado);
                new IceDecorator(PrevisaoBO.class).salvar(previsao);
                previsao = (Previsao) new IceDecorator(PrevisaoBO.class).buscaObjetoFK("associado", "codAssociado",
                        Integer.parseInt(comboAssociado.getValue().toString()));
            }
            setBean("Previsao", previsao);
            dataTable1Model.setWrappedData(previsao.getItemPrevisao());
        } catch (Exception e) {
        }
    }

    public void limparCampos() {

        FacesContext.getCurrentInstance().getExternalContext().getRequestMap().remove("Previsao");
        Previsao previsao = (Previsao) getBean("Previsao");
        dataTable1Model.setWrappedData(previsao.getItemPrevisao());

        campoQuantidade.setValue("0");
        voltaCombo();

        SelectItem prod = (SelectItem) produtos.get(0);
        comboProduto.setValue(prod.getValue());
        infoAssociado.setValue("");

        campoDataFim.resetValue();
        campoDataInicio.resetValue();
        campoTabelaDataInicio.setValue(campoDataInicio.getValue());
        campoTabelaDataFim.setValue(campoDataFim.getValue());
        habilitarCampos();
    }

    public void novaTabela() {

        listaExclusao.clear();
        FacesContext.getCurrentInstance().getExternalContext().getRequestMap().remove("Previsao");
        Previsao previsao = (Previsao) getBean("Previsao");
        dataTable1Model.setWrappedData(previsao.getItemPrevisao());
    }

    public void deletarLista() {
        while (!listaExclusao.isEmpty()) {
            try {
                new IceDecorator(ItemPrevisaoBO.class).excluir(listaExclusao.iterator().next());
                listaExclusao.remove(listaExclusao.iterator().next());
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println(e.getMessage());                
            }
        }
    }

    //ação dos campos do painel geral
    public void comboAssociado_processValueChange(ValueChangeEvent vce) throws Exception {
        if(!comboAssociado.getValue().equals("0")){
        Associado associado = (Associado) new IceDecorator(AssociadoBO.class).retornaObjetoCodigo("codAssociado", Integer.parseInt(comboAssociado.getValue().toString()));
        infoAssociado.setValue(associado.getNome().toString());
        }
    }

    public void campoDataInicio_processValueChange(ValueChangeEvent vce) {
        campoTabelaDataInicio.setValue(campoDataInicio.getValue());
    }

    public void campoDataFim_processValueChange(ValueChangeEvent vce) {
        campoTabelaDataFim.setValue(campoDataFim.getValue());
    }

    //ações dos botões de tela
    public String botaoAdicionarProduto_action() throws Exception {
        if(comboAssociado.getValue().equals("0")){
            panelPopup1Bean1.setShowDraggablePanel(true);
            return null;
        }
        comboAssociado.setDisabled(true);
        campoDataFim.setDisabled(true);
        campoDataInicio.setDisabled(true);

        Previsao previsao = (Previsao) getBean("Previsao");
        previsao.setAssociado(new Associado(Integer.parseInt(comboAssociado.getValue().toString())));
        try {
            ItemPrevisao itemPrev = (ItemPrevisao) getBean("ItemPrevisao");
            itemPrev.setQuantidadeAuxiliar(itemPrev.getQuantidade());
            Produto produto = (Produto) new DAO(Produto.class).consultarObjetoCodigo("codProduto", Integer.parseInt(comboProduto.getValue().toString()));
            itemPrev.setProduto(produto);
            itemPrev.setPrevisao(previsao);
            previsao.adicionarItemPrevisao(itemPrev);
            items.clear();
            items.addAll(previsao.getItemPrevisao());

            setBean("Previsao", previsao);

            dataTable1Model.setWrappedData(items);

            FacesContext.getCurrentInstance().getExternalContext().getRequestMap().remove("ItemPrevisao");            
            return null;

        } catch (Exception e) {
            return null;
        }
    }

    //ações dos botões de tabela
    public String botaoTabelaExcluir_action() {
        Previsao previsao = (Previsao) getBean("Previsao");
        List<ItemPrevisao> itemPrevisao = (List<ItemPrevisao>) previsao.getItemPrevisao();
        listaExclusao.add(itemPrevisao.remove(dataTable1Model.getRowIndex()));
        previsao.setItemPrevisao(itemPrevisao);
        setBean("Previsao", previsao);
        dataTable1Model.setWrappedData(previsao.getItemPrevisao());
        return null;
    }

    //ação dos botões de pop ups
    public String botaoSairNao_action() {
        painelSair.setRendered(false);
        return null;
    }

    public String botaoSairSim_action() {
        //return null means stay on the same page
        return "menu";
    }

    public String botaoConsultaCancelar_action() {
        painelConsulta.setRendered(false);
        return null;
    }

    public String botaoConsultaOk_action() throws Exception {
        limparCampos();
        comboAssociado.setValue(comboAssociadoConsulta.getValue());
        Associado associado = (Associado) new IceDecorator(AssociadoBO.class).retornaObjetoCodigo("codAssociado", Integer.parseInt(comboAssociadoConsulta.getValue().toString()));
        infoAssociado.setValue(associado.getNome().toString());
        comboAssociadoConsulta.resetValue();
        montaTabelaAssociado();
        desabilitarCampos();        
        painelConsulta.setRendered(false);        
        return null;
    }

    public String botaoAvisoSim_action() {
        painelAviso.setRendered(false);
        return null;
    }

    public String botaoMensagemOk_action() {
        painelMensagem.setRendered(false);
        return null;
    }

    public String botaoAvisoNao_action() {
        painelAviso.setRendered(false);
        painelConsulta.setRendered(false);
        return null;
    }

    public String botaoAtencaoOk_action() {
        //return null means stay on the same page
        return null;
    }

    //ações dos botões do painel de opções
    public String botaoNovo_action() {
        campoQuantidade.setValue(0);
        limparCampos();
        novaTabela();
     //   habilitarCampos();
        return null;
    }    

    public String botaoConsultar_action() {        
        associadosConsulta.clear();        
        setAssociadosConsulta(new IceDecorator(AssociadoBO.class).montaCombo());        
        associadosConsulta.add(0, new SelectItem("0", "Selecione"));        
        comboAssociadoConsulta.setValue(0);
        previsoesConsulta.clear();
        previsoesConsulta.add(0, new SelectItem("0", "Selecione"));        
        comboPrevisaoConsulta.setValue(0);
        habilitarCampos();        
        painelConsulta.setRendered(true);
        return null;
    }

    public String botaoAlterar_action() {
        habilitarCampos();
        return null;
    }

    public String botaoImprimir_action() {
        //return null means stay on the same page
        return null;
    }

    public String botaoCadastrar_action() throws Exception {

        Previsao previsao = (Previsao) getBean("Previsao");
        previsao.setDataFim(new ValidaFacade().consertarData((Date)campoDataFim.getValue()));
        previsao.setDataInicio(new ValidaFacade().consertarData((Date)campoDataInicio.getValue()));

        try {
            if (previsao.getCodPrevisao() == 0) {
                items.clear();
                items.addAll(previsao.getItemPrevisao());
                previsao.getItemPrevisao().clear();
                int cod = new DAO(Previsao.class).salvarId(previsao);
                previsao.setCodPrevisao(cod);

                while (!items.isEmpty()) {
                    items.iterator().next().setPrevisao(previsao);
                    previsao.adicionarItemPrevisao(items.iterator().next());
                    items.remove(items.iterator().next());
                }
                new DAO(Previsao.class).salvar(previsao);
            } else {
                new DAO(Previsao.class).salvar(previsao);
            }
            deletarLista();
            comboAssociado.setDisabled(false);
            limparCampos();
            desabilitarCampos();
            painelMensagem.setRendered(true);

        } catch (Exception e) {

            painelMensagem.setRendered(true);
            msgMensagemResultado.setValue("Erro ao realizar operação");
            
        }
        return null;
    }

    public String botaoLimpar_action() {
        limparCampos();
        return null;
    }

    public String botaoSair_action() {
        painelSair.setRendered(true);
        return null;
    }

    public void validateData(FacesContext context, UIComponent component, Object value) {

        FacesMessage msg = new FacesMessage();
        try {
            Date data = (Date) value;
            if (data.before((Date) campoDataInicio.getValue())) {
                msg.setSummary("*");
                msg.setDetail("Data entrega posterior a data do pedido");
                throw new ValidatorException(msg);
            }
        } catch (Exception e) {
            throw new ValidatorException(msg);
        }
    }

    public void comboAssociadoConsulta_processValueChange(ValueChangeEvent vce) {
        this.setPrevisoesConsulta(new IceDecorator(PrevisaoBO.class).montaComboPrevisao(Integer.parseInt(comboAssociadoConsulta.getValue().toString())));
        previsoesConsulta.add(0, new SelectItem("0", "Selecione"));
        comboPrevisaoConsulta.setValue(0);
    }

    public String botaoErroOk_action() {
        panelPopup1Bean1.setShowDraggablePanel(false);
        return null;
    }


    public void campoQuantidade_validate(FacesContext context, UIComponent component, Object value) {
        new ValidaFacade().validateNumeros(context, component, value, PrevisaoBO.class, parar);
    }
}

